package com.quanyan.place.web;

import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.quanyan.api.APIResponse;
import com.quanyan.place.entity.apireq.PlaceRefundDetail;
import com.quanyan.place.entity.apireq.PlaceRefundInfo;
import com.quanyan.place.entity.apireq.ReqPlaceOrderSearchParam;
import com.quanyan.place.entity.apireq.ReqPlaceOrderSearchVo;
import com.quanyan.place.entity.apireq.ReqPlaceRefundParam;
import com.quanyan.place.service.PlaceOrderAdminService;

/**
 * 商家端订单管理模块
 * 
 * @des 订单列表请求参数
 * @author tim
 * @date 2017年4月12日
 */

@RestController
@RequestMapping("/admin/api/place/pcorder")
public class PlaceOrderAdminController {
	private final static Logger logger = LoggerFactory.getLogger(PlaceOrderAdminController.class);

	@Autowired
	PlaceOrderAdminService placeOrderAdminService;

	/**
	 *  1.获取订单管理列表
	 * @param reqPlaceOrderSearchParam
	 * @return
	 */
	@RequestMapping(value = "/placeOrderList", method = RequestMethod.POST, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	public APIResponse placeOrderList(@RequestBody @Valid ReqPlaceOrderSearchParam reqPlaceOrderSearchParam) {
		return APIResponse.returnSuccess(placeOrderAdminService.getPlaceOrderList(reqPlaceOrderSearchParam));

	}

	/**
	 *  2.订单详情
	 * @param placeOrderId
	 * @return
	 */
	@RequestMapping(value = "/placeOrderDetail", method = RequestMethod.POST, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	public APIResponse placeOrderDetail(@RequestParam(value = "placeOrderId", required = true) String placeOrderId) {

		return null;

	}

	/**
	 * 3.申请退款
	 * 
	 * @param isPart(0:全部退,1:部分退）
	 * @param useType
	 *            (包括门票、商品、场地三种退款处理)
	 * @param placeOrderId
	 * @return
	 */
	@RequestMapping(value = "/applyPartRefund", method = RequestMethod.POST, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	public APIResponse applyPartRefund(@RequestParam(value = "isPart", required = true) Integer isPart,
			@RequestParam(value = "useType", required = true) Integer useType,
			@RequestParam(value = "placeOrderId", required = true) String placeOrderId,
			@RequestParam(value = "operateUid", required = true) Integer operateUid,
			@RequestParam(value = "customerId", required = true) Integer customerId,
			@RequestParam(value = "placeId", required = true) Integer placeId) {
	    return null;
	}
	
	
	/**
	 * 4.订单退款
	 * @param reqPlaceRefundInfo
	 * @return
	 */
	@RequestMapping(value = "/placeOrderRefund", method = RequestMethod.POST, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	public APIResponse placeOrderRefund(@RequestBody @Valid PlaceRefundInfo reqPlaceRefundInfo) {
		try {
				return placeOrderAdminService.placeRefund(reqPlaceRefundInfo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return APIResponse.returnFail("退款失败");
	}
	
	/**
	 * 5.订单付款
	 * 
	 * @param reqPlaceOrderSearchVo
	 * @return
	 */
	@RequestMapping(value = "/payPlaceOrder", method = RequestMethod.POST, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	public APIResponse payPlaceOrder(@RequestBody @Valid ReqPlaceOrderSearchVo reqPlaceOrderSearchVo) {
		return null;

	}

	/**
	 * 6.取消订单
	 * 
	 * @param reqPlaceOrderSearchVo
	 * @return
	 */
	@RequestMapping(value = "/cancelPlaceOrder", method = RequestMethod.POST, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	public APIResponse cancelPlaceOrder(@RequestBody @Valid ReqPlaceOrderSearchVo reqPlaceOrderSearchVo) {
		return null;

	}

	/**
	 * 7.退款提示信息
	 * 
	 * @param reqPlaceOrderSearchVo
	 * @param isPart(0:全部退,1:部分退）
	 * @return
	 */
	@RequestMapping(value = "/refundPartOrderHistoryTip", method = RequestMethod.POST, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	public APIResponse refundOrderTip(@RequestBody @Valid ReqPlaceOrderSearchVo reqPlaceOrderSearchVo) {
		return null;

	}

	/**
	 * 8.实付提示信息
	 * 
	 * @param reqPlaceOrderSearchVo
	 * @param isPart(0:全部退,1:部分退）
	 * @return
	 */
	@RequestMapping(value = "/payRealOrderTip", method = RequestMethod.POST, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	public APIResponse payRealOrderTip(@RequestBody @Valid ReqPlaceOrderSearchVo reqPlaceOrderSearchVo) {
		return null;

	}

	/**
	 *9 查看该固定场所有子订单
	 * 
	 * @param customerName
	 * @param mobile
	 * @return
	 */
	@RequestMapping(value = "/searchFixedChildOrder", method = RequestMethod.POST, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	public APIResponse searchFixedAllChildOrder(
			@RequestParam(value = "placeOrderId", required = true) String placeOrderId,
			@RequestParam(value = "pageSize", required = true) Integer pageSize,
			@RequestParam(value = "pageNum", required = true) Integer pageNum) {
		// TODO Auto-generated method stub
		ReqPlaceOrderSearchParam orderSearchParam = new ReqPlaceOrderSearchParam();
		orderSearchParam.setPlaceOrderId(placeOrderId);
		orderSearchParam.setPageSize(pageSize);
		orderSearchParam.setPageNum(pageNum);
		return APIResponse.returnSuccess(placeOrderAdminService.searchFixedAllChildOrder(orderSearchParam));

	}

	/**
	 * 10 退款明细
	 * 
	 * @param reqPlaceOrderSearchVo
	 * @return
	 */
	@RequestMapping(value = "/refundItem", method = RequestMethod.POST, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	public APIResponse refundItem(@RequestBody @Valid ReqPlaceRefundParam refundParam) {
		return APIResponse.returnSuccess(placeOrderAdminService.getPlaceRefundDetailList(refundParam));

	}

	/**
	 * 11 查看退款详情
	 * 
	 * @param reqPlaceOrderSearchVo
	 * @return
	 */
	@RequestMapping(value = "/refundDetail", method = RequestMethod.POST, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	public APIResponse refundDetail(@RequestBody @Valid PlaceRefundDetail placeRefundDetail) {
		 return APIResponse.returnSuccess(placeOrderAdminService.searchPlaceRefundDetail(placeRefundDetail.getRefundNo()));

	}

	/**
	 * 12.加星订单
	 * 
	 * @param placeOrderId
	 * @param starStatus
	 * @return
	 */
	@RequestMapping(value = "/addStarOrder", method = RequestMethod.POST, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	public APIResponse addStarOrder(@RequestParam(value = "placeOrderId", required = true) String placeOrderId,
			@RequestParam(value = "starStatus", required = true) Integer starStatus) {
		Map<String, Object> params = new HashMap<>();
		params.put("placeOrderId", placeOrderId);
		params.put("starStatus", starStatus);// '加星状态 （0：默认没有；1:加星;）

		int result = placeOrderAdminService.addOrderStar(params);
		if (result > 0) {
			logger.info("订单加星成功");
			return APIResponse.returnSuccess("订单加星成功");
		} else {
			logger.info("订单加星失败");
			return APIResponse.returnFail("加星失败");
		}

	}

	/**
	 * 13.修改散客订单信息
	 * 
	 * @param customerName
	 * @param mobile
	 * @return
	 */
	@RequestMapping(value = "/updateCasualMemberOrder", method = RequestMethod.POST, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	public APIResponse updateCasualMemberOrder(
			@RequestParam(value = "customerName", required = true) String customerName,
			@RequestParam(value = "mobile", required = true) String mobile,
			@RequestParam(value = "placeOrderId", required = true) String placeOrderId) {
		// TODO Auto-generated method stub
		Map<String, Object> params = new HashMap<>();
		params.put("customerName", customerName);
		params.put("mobile", mobile);
		params.put("placeOrderId", placeOrderId);
		int result = placeOrderAdminService.updateCasualMemberOrder(params);
		if (result > 0) {
			logger.info("修改散客订单信息成功");
			return APIResponse.returnSuccess("修改散客订单信息成功");
		} else {
			logger.info("修改散客订单信息失败");
			return APIResponse.returnFail("修改散客订单信息失败");
		}

	}

}
